<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :show="show" @click-show="show = !show" :fixed="true" :back="true" color="#fff" title="问题列表"></uni-navbar></view>

		<view class="filter-box" v-if="show">
			<view class="form-box">
				<u-form ref="uForm">
					<u-form-item class="form-item" label="问题标题">
						<!-- block -->
						<u-input v-model="masses.title" type="text" placeholder="请输入问题标题" />
					</u-form-item>
					<u-form-item class="form-item" label="提交人">
						<!-- block -->
						<u-input v-model="masses.userName" type="text" placeholder="请输入提交人" />
					</u-form-item>
					<u-form-item class="form-item" label="常委">
						<!-- block -->
						<u-input v-model="masses.cwName" type="text" placeholder="请输入常委姓名" />
					</u-form-item>
				</u-form>
			</view>
			<view class="fiexd-btm">
				<view class="flex-box">
					<u-button class="form-btn" type="default" shape="circle" @click="resert">清除</u-button>
					<u-button class="form-btn" type="error" shape="circle" @click="filter">确定</u-button>
				</view>
			</view>
		</view>

		<view class="search-view"><u-search class="search-input" shape="square" bg-color="#fff" v-model="masses.title" :show-filter="true" @search="search" @custom="custom"></u-search></view>
		<u-tabs class="uTabs" :list="tabsList" :current="tabsCurrent" @change="tabsChange" :is-scroll="true"></u-tabs>

		<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '#fff' }">
			<view class="list-info_item" v-for="(item, index) of massesList" :key="item.id">
				<view class="list-info_02" @click="navTo('/pagesA/condi/detail_zw?id=' + item.id)">
					<view class="left">
						<view class="info-title">{{ item.title }}</view>
						<view class="info-btm">
							<view class="left-name">{{ item.submitUserName }}</view>
							<view class="line"></view>
							<view class="left-time">{{ item.submitTime }}</view>
						</view>
					</view>
					<view class="right" :style="'color:' + colorMap[item.nodeStatus]">{{ item.nodeStatusName }}</view>
				</view>
				<view class="info-btn">
					<u-button class="sub-btn" v-if="item.nodeStatus == 'wait' && $sysBtns('shuangl_edit3')" type="success" plain size="medium" shape="circle" @click="edit(item)">编辑</u-button>
					<u-button class="sub-btn" v-if="item.nodeStatus == 'wait' && $sysBtns('shuangl_cl3')" type="error" plain size="medium" shape="circle" @click="handle(item)">处理</u-button>
					<u-button class="sub-btn" v-if="['1', '2', '3'].indexOf(item.nodeStatus) > -1 && $sysBtns('shuangl_bj3')" type="error" plain size="medium" shape="circle" @click="handle(item)">办结</u-button>
					<u-button class="sub-btn" v-if="['4', '5'].indexOf(item.nodeStatus) > -1 && $sysBtns('shuangl_bj3')" type="error" plain size="medium" shape="circle" @click="handle(item)">办结</u-button>
				</view>
			</view>
			<u-loadmore :status="loading" />
		</view>

		<view class="btn-add" @click="navTo('/pagesA/condi/add_zw')"><u-icon name="plus" color="#fff" size="38"></u-icon></view>

		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker
	},
	data() {
		return {
			scrollTop: 0,
			show: false,
			tabIndex: 0,
			tabsCurrent: 0,
			tabsList: [
				{
					name: '所有',
					value: ''
				},
				{
					name: '待处理',
					value: 'wait'
				},
				// {
				// 	name: '转建议',
				// 	value: '2'
				// },
				// {
				// 	name: '转议案',
				// 	value: '3'
				// },
				{
					name: '转代表室',
					value: '5'
				},
				{
					name: '不予接收',
					value: '6'
				},
				{
					name: '已办结',
					value: 'end'
				}
			],
			statusMap: {
				0: '所有',
				2: '转建议',
				3: '转议案',
				5: '转代表室',
				6: '不予接收'
			},
			colorMap: {
				0: '#303133',
				1: '#3396fb',
				2: '#ff9900',
				3: '#19be6b',
				4: '#ce2d2c',
				5: '#909399',
				6: '#2b85e4'
			},
			title: '',

			type: '',
			typeShow: false,
			typeList: [],

			createTime: '',
			endTime: '',

			massesList: [],

			startTime: '',
			endTime: '',

			masses: {
				title: '',
				numCode: '',
				cwName: '',
				dateRangeSea: '',
				userName: '',
				nodeStatus: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad(option) {
		this.loadLogin();
		if (option && option.status) {
			this.tabsCurrent = option.status;
			this.masses.nodeStatus = this.tabsList[option.status].value;
		}
		this.loadData();
		this.loadDict('cw_dict_type', 'typeList');
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		dateRangeSea: {
			get: function() {
				let time = this.startTime + ' - ' + this.endTime;
				if (this.startTime == '' || this.endTime == '') {
					return '';
				} else {
					return time;
				}
			},
			set: function(e) {
				this.startTime = '';
				this.endTime = '';
			}
		},
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.sysUser) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;
				this.roleCodes = this.userInfo.roleCodes.join(',');
				this.masses.userId = this.userInfo.sysUser.id;
				this.masses.clientCode = this.userInfo.sysUser.clientCode;

				this.pageNum = 1;
				this.pageSize = 10;
			} else {
				this.$logout();
			}
		},
		loadDict(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this[list] = [];
				res.data.forEach((item) => {
					this[list].push({
						value: item.dictValue,
						label: item.dictName
					});
				});
			});
		},
		loadData(type = 'add') {
			if (type == 'add' && this.loading == 'loading') {
				return false;
			}
			if (type == 'add' && this.loading == 'nomore') {
				return false;
			}

			this.loading = 'loading';
			if (type != 'add') {
				this.pageNum = 1;
			}
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				currentUserId: this.userId,
				clientCode: this.clientCode,
				role: this.roleCodes,
				...this.masses
			};
			let url = '/question/zwh/list';

			this.$request(url, data, {
				load: true
			}).then((res) => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.massesList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.massesList = this.massesList.concat(res.data.records);
				if (this.massesList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		typeConfirm(e) {
			this.type = e[0].label;
			this.masses.processType = e[0].value;
		},
		timeChange({ date, dt }, idx) {
			this[idx] = date.year + '-' + date.month2 + '-' + date.day2;
		},
		tab(e) {
			this.tabIndex = e;
			this.loadData('refresh');
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.tabIndex = index * 10 + 10;
			this.typeFunc(this.tabsList[index].value);
		},
		del(e) {
			let url = '/minqingzhitong/msPublicOpinion/del';
			let data = {
				id: e.id
			};

			uni.showModal({
				title: '提示',
				content: '确认删除？',
				success: (res) => {
					if (res.confirm) {
						this.$request(url, data, {
							load: true
						}).then((res) => {
							this.$msg(res.msg);
							this.loadData('refresh');
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		filter() {
			if (this.startTime == '' && this.endTime != '') {
				this.$msg('请选择开始时间');
				return false;
			}
			if (this.startTime != '' && this.endTime == '') {
				this.$msg('请选择结束时间');
				return false;
			}
			let arr = this.startTime.split('-');
			let startTime = new Date(arr[0], arr[1], arr[2]).getTime();

			let arrs = this.endTime.split('-');
			let endTime = new Date(arrs[0], arrs[1], arrs[2]).getTime();

			if (startTime >= endTime) {
				this.$msg('开始时间应小于结束时间');
				return false;
			}

			this.masses.dateRangeSea = this.dateRangeSea;

			this.show = false;
			this.loadData('refresh');
		},
		resert() {
			this.show = false;
			this.masses.title = '';
			this.masses.numCode = '';
			this.masses.cwName = '';
			this.masses.dateRangeSea = '';
			this.masses.userName = '';
			this.loadData('refresh');
		},
		search(e) {
			this.masses.title = e;
			this.loadData('refresh');
		},
		custom(e) {
			// this.masses.title = e;
			// this.loadData('refresh');
			uni.navigateTo({
				url: '/pagesA/condi/list_zw-search',
				events: {
					loadData: (data) => {
						Object.assign(this.masses, data);
						this.loadData('refresh');
					}
				},
				success: (res) => {
					res.eventChannel.emit('masses', this.masses);
				}
			});
		},
		typeFunc(e) {
			this.masses.nodeStatus = e || '';
			this.loadData('refresh');
		},
		edit(item) {
			uni.navigateTo({
				url: '/pagesA/condi/add_zw?id=' + item.id
			});
		},
		handle(item) {
			uni.navigateTo({
				url: '/pagesA/condi/detail_zw?id=' + item.id
			});
		},
		navTo(url) {
			uni.navigateTo({
				url: url
			});
		}
	}
};
</script>

<style lang="scss">
.list-box {
	border-top: 20rpx solid $u-border-color;
}

.list-info_02 {
	.right {
		width: 200rpx;
	}
}

.filter-box {
	position: fixed;
	left: 0;
	top: 0;
	bottom: 0;
	right: 0;
	z-index: 998;
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
